package light.head.framework.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import light.head.config.ConfigParm;
import light.head.constant.Sys;

/**
 * 下载
 */
public class DownLoadUtils {
	
	/**
	 * @param upload_path 文件夹相对路径
	 * @param name	原始文件名
	 * @param uuid	
	 * @param ext 后缀名	
	 */
	public static void downLoad(String upload_path, String name, String uuid, String ext ,HttpServletResponse response){
		String fileName = name + Sys.DOT + ext; 
		response.setContentType("multipart/form-data");  
        try {
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));  
			File file = new File(uuid + Sys.DOT + ext);  
			String path = ConfigParm.getProjectPath() + upload_path;  
			//path = path.replace("/", "\\");
			InputStream in = new FileInputStream(path + file);  
			OutputStream os = response.getOutputStream();  
			byte[] b = new byte[1024 * 1024]; 
			int length;  
			while ((length = in.read(b)) > 0) {  
			    os.write(b, 0, length);  
			}  
			in.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}
